package com.faithbj.device.service.impl;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.faithbj.device.dao.ReservationDao;
import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Members;
import com.faithbj.device.model.entity.Reservation;
import com.faithbj.device.service.ReservationService;
import com.faithbj.device.utils.DateUtils;

@Service("reservationService")
public class ReservationServiceImpl extends BaseServiceImpl<Reservation, String> implements ReservationService
{
	@Resource(name = "reservationDao")
	public void setBaseDao(ReservationDao reservationDao) {
		super.setBaseDao(reservationDao);
	}
	
	@Resource(name = "reservationDao")	
	ReservationDao reservationDao;
	/**
	 * 根据用户id，查询所有该用户预约到的仪器
	 */
	@Override
	public Pager findReservationByMemberId(Pager pager,Members member){
		if (pager == null) 
			pager = new Pager();
		String hql="from Reservation as r where r.member.id =? order by  r.createDate desc";
		String counthql="select count(*) from Reservation as r where r.member.id =?";
		Long count=reservationDao.count(counthql,  member.getId());
//		String hql="select r.device.classifyName,r.beginDate,r.endDate,r.createDate from Reservation as r where r.member.id =?";
		pager=reservationDao.findPager(pager, hql, count.intValue(), member.getId());
		return pager;
	}
	
	/**
	 * 查询仪器预约后的费用结算列表 
	 */
	@Override
	public Pager findReservationCost(Pager pager){
		if (pager == null) 
			pager = new Pager();
		String hql="from Reservation as r order by  r.createDate desc";
		String counthql="select count(*) from Reservation as r";
		Long count=reservationDao.count(counthql);
		pager=reservationDao.findPager(pager, hql, count.intValue());
		return pager;
	}
	
	/**
	 * 根据仪器id，查询所有截至今天的仪器预约情况
	 * @param date 要预约的时间
	 * @param devicesId 设备ID
	 */
	@Override
	public List<Reservation> findReservationByDeviceId(Date date,String devicesId){
		String hql="from Reservation t where t.device.id =? and t.endDate>? and t.endDate<?";
		List<Reservation> list=reservationDao.findList(hql,devicesId,date,DateUtils.getDaysAfter(date,1));
		return list;
	}
	
	
	/**
	 * 查询仪器预约后的费用结算列表 
	 */
	@Override
	public Pager findReserveCostByExample(Reservation reservation,Integer pageNumber,Integer pageSize){
		reservation.handleBlankProperties();
		Pager pager=reservationDao.findReserveCostByExample(reservation, pageNumber, pageSize);
		return pager;
	}
	
}
